import datetime
import os.path
import sys

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

#创建浏览器对象
class Driver:
    driver = ""
    def __init__(self):
        options = webdriver.ChromeOptions()
        options.add_argument("--guest")

        self.driver = webdriver.Chrome(service = Service(ChromeDriverManager().install()), options = options)

    #创建屏幕截图
    def screenshot(self):
        root = "../img"
        dirname = datetime.datetime.now().strftime("%y-%m-%d")

        # 拼接完整路径
        dirpath = os.path.join(root, dirname)

        # 确保目录存在
        os.makedirs(dirpath, exist_ok=True)

        # 生成文件名：调用函数名 + 时间戳
        caller = sys._getframe().f_back.f_code.co_name
        filename = f"{caller}_{datetime.datetime.now().strftime('%H%M%S')}.png"

        # 保存截图
        filepath = os.path.join(dirpath, filename)
        self.driver.save_screenshot(filepath)

questionnaireDriver = Driver()
